home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / file / strings-1.0.lha / strings.c < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  92 lines

  1. /*
  2.  * Strings 1.0
  3.  *
  4.  * Public Domain 1992 Dan Zerkle
  5.  *
  6.  * See documentation file for usage.
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. #define BUFSIZE 1024
  12.  
  13. static char *title0 = "\0Strings 1.0 February 1992";
  14. static char *title1 = "\0This program Public Domain 1992 Dan Zerkle";
  15. static char *title2 = "\0Please e-mail zerkle@cs.ucdavis.edu";
  16.  
  17. main (argc, argv)
  18.   int argc; char **argv;
  19.  
  20. {
  21.   int asccount=0;
  22.   int ccount=0;
  23.   int in;
  24.   int minlen=6;
  25.   char string[BUFSIZE];
  26.   int newline='1';
  27.  
  28.   FILE *infile;
  29.  
  30.   while ((argc>1) && (argv[1][0]=='-'))
  31.     {
  32.       if (argv[1][1]=='n')
  33.         newline=0;
  34.       else if ((argv[1][1]>='0') && (argv[1][1]<='9'))
  35.         minlen=(-atoi(argv[1]));
  36.       else
  37.         {
  38.           fprintf(stderr,"Unknown option %s\n",argv[1]);
  39.           fprintf(stderr,"USAGE: strings [-n] [-<minsize>] [<filename>]\n");
  40.           exit(1);
  41.         }
  42.  
  43.       argc--;argv++;
  44.     } /* if */
  45.  
  46.   if (minlen>=BUFSIZE)
  47.     minlen=BUFSIZE-1;
  48.  
  49.   if (argc > 1)
  50.     {
  51.       if (!(infile=fopen(argv[1],"rb")))
  52.         {
  53.           fprintf(stderr,"Can't open file %s\n",argv[1]);
  54.           exit(1);
  55.         }
  56.     } /* if */
  57.   else
  58.     infile=stdin;
  59.  
  60.   string[minlen-1]=0;
  61.  
  62.   while((in=getc(infile))!=EOF)
  63.     {
  64.       ccount++;
  65.       if ((in < ' ') || (in > '~'))
  66.         {
  67.           if ((asccount>=minlen) && newline)
  68.             putchar('\n');
  69.           asccount=0;
  70.         } /* if */
  71.  
  72.       else
  73.         {
  74.           if (++asccount <= minlen-1)
  75.             string[asccount-1]=in;
  76.           else if (asccount == minlen)
  77.             {
  78.               fputs(string,stdout);
  79.               putchar(in);
  80.             } /* else if */
  81.           else
  82.             putchar(in);
  83.  
  84.         }  /* else */
  85.     } /* while */
  86.  
  87.   if (asccount && newline)
  88.     putchar('\n');
  89.  
  90.   exit(0);
  91. } /* main */
  92.